package com.lxl.redis1.controller;


import com.lxl.redis1.pojo.Book;
import com.lxl.redis1.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    @ResponseBody
    @GetMapping("/setValue")
    public String setVlaue(){ //存储字符串类型的key-value
        bookService.setValue("redis","Spring Date Redis");
        return "保存键值对成功！";
    }

    @ResponseBody
    @GetMapping("/getValue")
    public String getValue(){ //通过键获取字符串类型的值
        ModelAndView mv=new ModelAndView();
        return bookService.getValue("redis");

    }


    @GetMapping("/saveBook")
    @ResponseBody
    public String saveBook(){
        Book book=new Book(1,"C语言程序设计",50.0,"计算机",100,"101.jpg","","zhangsan",50);
        bookService.saveBook(book);
        return "保存一本书成功！";
    }

    @GetMapping("/findBook")
    public ModelAndView findBookById(){
        ModelAndView mv=new ModelAndView();
        Book book=bookService.findBookById(1);
        mv.addObject("book",book);
        mv.setViewName("book");
        return mv;
    }

    @GetMapping("/saveBooks")
    @ResponseBody
    public String saveBooks(){
        Book book1=new Book(1,"C语言程序设计",50.0,"计算机",100,"101.jpg","","zhangsan",50);
        Book book2=new Book(2,"java语言程序设计",60.0,"计算机",100,"102.jpg","","zhangsan",50);
        Book book3=new Book(3,"python语言程序设计",70.0,"计算机",100,"103.jpg","","zhangsan",50);
        List<Book> books=new ArrayList<Book>();
        books.add(book1);
        books.add(book2);
        books.add(book3);
        bookService.saveBooks("101",books);
        return "保存多本书成功！";
    }

    @GetMapping("/findBookById/{id}")
    public ModelAndView findBookById(@PathVariable("id") int id){
        ModelAndView mv=new ModelAndView();
        List<Book> books=bookService.findBooks("101");
        Book book=new Book();
        if(books!=null&&books.size()>0){
            book=books.get(id-1);
        }
        mv.addObject("book",book);
        mv.setViewName("book");
        return mv;
    }

    @GetMapping("/findBooks")
    public ModelAndView findBooks(){
        ModelAndView mv=new ModelAndView();
        List<Book> books=bookService.findBooks("101");
        mv.addObject("books",books);
        mv.setViewName("books");
        return mv;
    }

    @GetMapping("/deleteBook/{id}")
    public ModelAndView deleteBook(@PathVariable("id") int id){
        ModelAndView mv=new ModelAndView();
        bookService.deleteBook(id);
        List<Book> books=bookService.findBooks("101");
        books.remove(id-1);
        bookService.saveBooks("101",books);
        mv.addObject("books",books);
        mv.setViewName("books");
        return mv;
    }

}
